<?php
if( !defined('IN_INDEX') )
{
	die('This page is hidden.');
}
if(!checkSession())
{
	function redirect($url)
			{
			?>
				<script type="text/javascript">
					{
					window.location = "<?php echo $url; ?>";
					}
				</script>
			<?php 
			}
			redirect("index.php?action=login");
	exit;
}
else
{
	if(isset($_REQUEST['id']))
	{
		$USER_PROFILE = new User('id', $_REQUEST['id']);
		$page_url = "index.php?action=profile&id=" . $_REQUEST['id'] . "&sub=stats";
	}
	else
	{
		$USER_PROFILE = $USER;
		$page_url = "index.php?action=profile&sub=stats";
	}
}
include('inc/functions_blog.php');
include('Charts/Includes/FusionCharts.php');
$blogs = getTrainingList(" WHERE user_id='" . $USER_PROFILE->getId() . "'");
$dateformat = $CONFIG['date_format'];
?>
<script src="./jscript/prettyphoto/js/jquery-1.4.4.min.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" href="./jscript/prettyphoto/css/prettyPhoto.css" type="text/css" media="screen" charset="utf-8" />
<script src="./jscript/prettyphoto/js/jquery.prettyPhoto.js" type="text/javascript" charset="utf-8"></script>
<SCRIPT LANGUAGE="Javascript" SRC="Charts/FusionCharts.js"></SCRIPT>
<div id='postpath'>
		<a title="<?php echo translate('gotofrontpage'); ?>" href="index.php"><?php echo translate('home'); ?></a>
		 &gt; <a href="index.php?action=blogs" title='<?php echo translate('showallblogs'); ?>'><?php echo translate('blog'); ?></a> 
         &gt; <a href="index.php?action=profile&id=<?php echo $USER_PROFILE->getId(); ?>"><?php echo ucf($USER_PROFILE->getFirstname()) . " " . ucf($USER_PROFILE->getLastname()); ?></a>
		&gt; <a href="index.php?action=profile&sub=stats&id=<?php echo $USER_PROFILE->getId(); ?>" title='<?php echo translate('stats'); ?>'><?php echo translate('stats'); ?></a>
        &gt; <a href="index.php?action=profile&sub=stats_lifted&id=<?php echo $USER_PROFILE->getId(); ?>" title='<?php echo translate('lifted'); ?>'><?php echo translate('lifted'); ?></a>
</div>
<?php 

if(count($blogs) > 0)
{

?>
<br />
<h2><?php echo translate('lifted'); ?></h2>
<br />
<?php echo translate('lifted_subtext'); ?>
<br /><br />
<form method="POST" action="index.php?action=profile&sub=stats_lifted&id=<?php echo $USER_PROFILE->getId(); ?>"> 

	<table border="0">
    <tr>
    <td>
	<b><?php echo ucf(translate('exercise')); ?>:</b>
    </td>
    <td>
	<select name="select_exercise">
     <?php
	$sql= mysql_query("SELECT lifts.exercise_id, lifts.training_id, training.training_id, training.user_id, exercise_name.ex_id, exercise_name.ex_name FROM lifts, training, exercise_name WHERE lifts.training_id=training.training_id AND exercise_name.ex_id=lifts.exercise_id AND training.user_id=".$USER_PROFILE->getId()." GROUP BY lifts.exercise_id ORDER BY ex_name");
	while($row = mysql_fetch_assoc($sql))
	{	
		$ex_id = $row['exercise_id'];
		$ex_name = getExercisename($row['exercise_id']);	
        echo '<option name="ex_id" value='. $ex_id.'';
		if(isset($_POST['select']) && $_POST['select_exercise']== $ex_id) { echo " selected='selected'"; }
		echo '>'. translate($ex_name) . '</option>';
    }
    ?>
	</select>
    </td>
        <td align="right">
        <b><?php echo ucf(translate('reps')); ?>:</b>
        </td>
        
        <td>
        <select name="reps">
        <option name="reps" value="1" <?php if(isset($_POST['select']) && $_POST['reps']== 1) { echo " selected='selected'"; } ?>>1RM</option>
        <option name="reps" value="2" <?php if(isset($_POST['select']) && $_POST['reps']== 2) { echo " selected='selected'"; } ?>>2RM</option>
        <option name="reps" value="3" <?php if(isset($_POST['select']) && $_POST['reps']== 3) { echo " selected='selected'"; } ?>>3RM</option>
        <option name="reps" value="4" <?php if(isset($_POST['select']) && $_POST['reps']== 4) { echo " selected='selected'"; } ?>>4RM</option>
        <option name="reps" value="5" <?php if(isset($_POST['select']) && $_POST['reps']== 5) { echo " selected='selected'"; } ?>>5RM</option>
        <option name="reps" value="6" <?php if(isset($_POST['select']) && $_POST['reps']== 6) { echo " selected='selected'"; } ?>>6RM</option>
        <option name="reps" value="7" <?php if(isset($_POST['select']) && $_POST['reps']== 7) { echo " selected='selected'"; } ?>>7RM</option>
        <option name="reps" value="8" <?php if(isset($_POST['select']) && $_POST['reps']== 8) { echo " selected='selected'"; } ?>>8RM</option>
        <option name="reps" value="9" <?php if(isset($_POST['select']) && $_POST['reps']== 9) { echo " selected='selected'"; } ?>>9RM</option>
        <option name="reps" value="10" <?php if(isset($_POST['select']) && $_POST['reps']== 10) { echo " selected='selected'"; } ?>>10RM</option>
        <option name="reps" value="11" <?php if(isset($_POST['select']) && $_POST['reps']== 11) { echo " selected='selected'"; } ?>>11RM</option>
        <option name="reps" value="12" <?php if(isset($_POST['select']) && $_POST['reps']== 12) { echo " selected='selected'"; } ?>>12RM</option>
        <option name="reps" value="13" <?php if(isset($_POST['select']) && $_POST['reps']== 13) { echo " selected='selected'"; } ?>>13RM</option>
        <option name="reps" value="14" <?php if(isset($_POST['select']) && $_POST['reps']== 14) { echo " selected='selected'"; } ?>>14RM</option>
        <option name="reps" value="15" <?php if(isset($_POST['select']) && $_POST['reps']== 15) { echo " selected='selected'"; } ?>>15RM</option>
        <option name="reps" value="16" <?php if(isset($_POST['select']) && $_POST['reps']== 16) { echo " selected='selected'"; } ?>>16RM</option>
        <option name="reps" value="17" <?php if(isset($_POST['select']) && $_POST['reps']== 17) { echo " selected='selected'"; } ?>>17RM</option>
        <option name="reps" value="18" <?php if(isset($_POST['select']) && $_POST['reps']== 18) { echo " selected='selected'"; } ?>>18RM</option>
        <option name="reps" value="19" <?php if(isset($_POST['select']) && $_POST['reps']== 19) { echo " selected='selected'"; } ?>>19RM</option>
        <option name="reps" value="20" <?php if(isset($_POST['select']) && $_POST['reps']== 20) { echo " selected='selected'"; } ?>>20RM</option>
        <option name="reps" value="0" <?php if(isset($_POST['select']) && $_POST['reps']== 0) { echo " selected='selected'"; } ?>><?php echo ucf(translate('max')); ?></option>
        </select>
        </td>
    
        <td>
        <input type="hidden" name="user_id" value="<?php echo $USER_PROFILE->getId(); ?>"  />
        <input type="submit" style="font-weight:bold;" name="select" value=" <?php echo ucf(translate('show')); ?> "  />
        </td>
    </table>
</form>

<?php 
if(isset($_POST['select']))
{
	$ex_id = $_POST['select_exercise'];
	$nr_reps = $_POST['reps'];
	$user_id = $_POST['user_id'];

if($nr_reps == 0)
{
	echo "<br /><h1>" . ucf(translate(getExercisename($ex_id))) . " - ". ucf(translate('max')). "</h1><br />";
}
else
{
	echo "<br /><h1>" . ucf(translate(getExercisename($ex_id))) . " - ". $nr_reps . "RM</h1><br />";
}
	
if($nr_reps == 0)
{
	// Get all the data
	$result = mysql_query("SELECT
	training.date, 
	lifts.kg,
	lifts.url,
	training.training_id,
	training.place,
	training.bodyweight
	FROM lifts, training
	WHERE training.training_id=lifts.training_id AND exercise_id= ". $ex_id ." AND training.user_id =". $user_id ."
	ORDER BY training.date, kg") or die(mysql_error());
	
	// Get the PB
	$result_pb = mysql_query("SELECT DISTINCT
	lifts.kg,
	training.training_id,
	training.date
	FROM lifts, training
	WHERE training.training_id=lifts.training_id AND exercise_id= ". $ex_id ." AND training.user_id =". $user_id ." ORDER BY -kg, training.date LIMIT 1") or die(mysql_error());
	
	// Get the last/badest lift
	$result_bpb = mysql_query("SELECT DISTINCT
	training.date, 
	lifts.kg,
	training.training_id
	FROM lifts, training
	WHERE training.training_id=lifts.training_id AND lifts.kg > 0 AND exercise_id= ". $ex_id ."  AND training.user_id =". $user_id ." ORDER BY training.date, kg LIMIT 1") or die(mysql_error());
}
else
{	
	// Get all the data
	$result = mysql_query("SELECT
	training.date, 
	lifts.kg,
	lifts.url,
	training.training_id,
	training.place,
	training.bodyweight
	FROM lifts, training
	WHERE training.training_id=lifts.training_id AND lifts.rep = ". $nr_reps ." and exercise_id= ". $ex_id ." AND training.user_id =". $user_id ."
	ORDER BY training.date, kg") or die(mysql_error());
	
	// Get the PB
	$result_pb = mysql_query("SELECT DISTINCT
	lifts.kg,
	training.training_id,
	training.date
	FROM lifts, training
	WHERE training.training_id=lifts.training_id AND lifts.rep = ". $nr_reps ." and exercise_id= ". $ex_id ." AND training.user_id =". $user_id ." ORDER BY -kg, training.date LIMIT 1") or die(mysql_error());
	
	// Get the last/badest lift
	$result_bpb = mysql_query("SELECT DISTINCT
	training.date, 
	lifts.kg,
	training.training_id
	FROM lifts, training
	WHERE training.training_id=lifts.training_id AND lifts.rep = ". $nr_reps ." and exercise_id= ". $ex_id ." AND training.user_id =". $user_id ."
	ORDER BY training.date LIMIT 1") or die(mysql_error());
}

$num = mysql_num_rows($result);
$i=1;
$strXML = "<chart caption='" . ucf(translate('lifted')) . " - " . ucf(translate(getExercisename($ex_id))) . " ". $nr_reps . "RM" . "' subCaption='" . ucf(translate('for')) . " " . getNameFromId($USER_PROFILE->getId()) . "' showBorder='1' formatNumberScale='0' numberSuffix='". $w_metric ."' baseFont='Arial' exportEnabled='1' exportAtClient='0' exportHandler='Charts/ExportHandlers/PHP/FCExporter.php' exportAction='download' exportTargetWindow='_blank' exportFileName='" . ucf(translate('lifted')) . "'>";

if($i <= $num)
{ 

		echo renderChart("Charts/ZoomLine.swf", "", $strXML, "".ucf(translate('lifted'))."", 900, 400, false, true);
		echo "<br /><table><tr><td>
			<table border='1'>";
		echo "<tr><th>" . translate('date') . "</th><th>". ucf(translate('place')) ."</th><th>" . ucf(translate('bodyweight')) . "</th><th>". translate('lifted') ."</th></tr>";
		
		$catstrXML = "<categories>"; 
        $datastrXML = "<dataset seriesName='". translate('lifted') ." " . translate('for') . " " . getNameFromId($USER_PROFILE->getId()) . " " . translate('i') . " " . strtolower(translate(getExercisename($ex_id))) . " " . $nr_reps . "RM" . "'>"; 
		
		// keeps getting the next row until there are no more to get
		while($row = mysql_fetch_array($result)) 
		{
			$catstrXML .= "<category label='" . formatDate($row['date'], $dateformat) . "' />";             
               if($w_metric == "kg"){ 
                   $datastrXML .= "<set value='" . $row['kg'] . "' />"; 
               }else{ 
                   $datastrXML .= "<set value='" . r_nr(calcFromKg($row['kg'])) . "' />"; 
               } 
			echo "<tr><td width='140' align='center'>";
			echo formatDate($row['date'], $dateformat);
			echo "</td><td width='160' align='center'>";
			echo $row['place'];
			echo "</td><td width='100' align='center'>";
			echo r_nr2(calcFromKg($row['bodyweight'])). $w_metric;
			echo "</td><td width='130' align='center'>"; 
			echo "<a href='index.php?action=profile&sub=show&tid=". $row['training_id'] ."'>";
			if($w_metric == "kg")
			{
			echo r_nr2(calcFromKg($row['kg'])). $w_metric . "</a>";
				
				if($row['url'] != "")
				{
					echo " <a href='" . $row['url'] . "' rel='prettyPhoto'><img alt='". translate('show_video') ."' src='img/video.png' /></a>";
				}
			}
			else
			{
			echo r_nr(calcFromKg($row['kg'])). $w_metric . "</a>";
				
				if($row['url'] != "")
				{
					echo " <a href='" . $row['url'] . "' rel='prettyPhoto'><img alt='". translate('show_video') ."' src='img/video.png' /></a>";
				}
			}
			echo "</td></tr>"; 
			$i++;
			$counter = $i;			
		}
		$catstrXML .= "</categories>"; 
        $datastrXML .= "</dataset>";
        $strXML .= $catstrXML . $datastrXML . "</chart>"; 
		$check = $counter-1; 
		echo translate('liftsfound') . ": " . $check . "<br /><br />";
		
		
		echo "</table></td>
			<td valign='top' style='padding-left: 50px; padding-top: 30px;'>
						<table border='0'>
								<tr>
									<td>";
								if($check >= 2)
								{
									while($row2 = mysql_fetch_array($result_pb)) 
									{
									echo translate('mycurrentpris') .  ": <b>" . r_nr(calcFromKg($row2['kg'])). $w_metric . "</b> (" . formatDate($row2['date'], $dateformat) . ")";
									$curpr = r_nr(calcFromKg($row2['kg']));
									$curdate = $row2['date'];
									}
									echo "
									</td>
								</tr>";
								
								echo "<tr>
									<td>";
									while($row3 = mysql_fetch_array($result_bpb)) 
									{
									echo translate('myfirstprwas') .  ": <b>" . r_nr(calcFromKg($row3['kg'])). $w_metric . "</b> (" . formatDate($row3['date'], $dateformat) . ")";
									$oldpr = r_nr(calcFromKg($row3['kg']));
									$olddate = $row3['date'];
									}
									echo "
									</td>
								</tr>";
								}
						echo "</table>
			</td>
		</tr>
		</table><br />";		
		if($check >= 2) //graph start
		{
			echo "<div style='display: none;'>";
			echo renderChart("Charts/ZoomLine.swf", "", $strXML, "".ucf(translate('lifted'))."", 900, 400, false, true);
			echo "</div>";
		}
		
		echo "<div class='fixed'>
							</div>
						</div>
						</div>
					<div class='fixed'>
					</div>
					<div id='footer'>
					<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
						<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
						</div>";
						?>
                        <script type="text/javascript" charset="utf-8">
						  $(document).ready(function(){
							$("a[rel^='prettyPhoto']").prettyPhoto();
						  });
						</script>
                        <?php
			exit;
			
}
else
{
	echo translate('noresultshof');
	echo "<div class='fixed'>
					</div>
				</div>
				</div>
			<div class='fixed'>
			</div>
			<div id='footer'>
			<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
				<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
                </div>";
				?>
                        <script type="text/javascript" charset="utf-8">
						  $(document).ready(function(){
							$("a[rel^='prettyPhoto']").prettyPhoto();
						  });
						</script>
                        <?php
	exit;
} 
}
}
else
{
  echo "<br /><br /><br /><br /><br /><br /><br /><br /><br />
		<center><h2>" . translate('addanewtrainingtostart') . "</h2></center>";
}
?>